package cn.dengta.webapp.user.vo;

import java.math.BigDecimal;
import java.util.Map;
import javax.annotation.Nullable;

import cn.dengta.common.util.Formats;
import cn.dengta.webapp.base.vo.AbstractVO;
import cn.dengta.webapp.user.entity.Prize;
import cn.dengta.webapp.user.model.GiftType;
import lombok.Getter;
import lombok.Setter;
import me.codeplayer.util.X;

@Getter
@Setter
public class PrizeVO extends AbstractVO<Prize> {

	/** 奖品名称 */
	private String name;
	/** 活动ID */
	private Long activityId;
	/** 活动 */
	private Map<String, Object> activity;
	/** 是否虚拟商品 */
	private Integer isVirtual;
	/** 礼品类型 */
	private GiftType giftType;
	/** 礼品ID */
	private Long giftId;
	/** 礼物图 */
	private String giftIcon;
	/** 价值（单位：元） */
	private Integer amount;
	/** 奖品总数量 */
	private Integer totalCount;
	/** 抽奖概率权重系数 */
	private Integer factor;
	/** 最小出现区间 */
	private Integer minTimes;
	/** 最大出现区间 */
	private Integer maxTimes;
	/** 已中奖数量（所有） */
	private Integer usedCount;
	/** 已中奖数量（免费钻） */
	private Integer usedFreeCount;
	/** 已中奖数量（免费次数） */
	private Integer usedFreeTimes;
	/** 交易总额度（所有） */
	private transient BigDecimal tradeTotalAmount;
	/** 本次交易使用的免费钻石 */
	private transient BigDecimal tradeTotalFreePart;
	/** 本次交易使用的充值钻石 */
	private transient BigDecimal tradeRechargeAmount;
	/** 获奖礼物总价值 */
	private Integer totalAmount;
	/** 奖品等级 */
	private Integer level;

	public static PrizeVO from(@Nullable Prize source) {
		return copy(source, PrizeVO::new);
	}

	public static PrizeVO fromHead(PrizeVO source) {
		PrizeVO vo = new PrizeVO();
		vo.setUsedCount(X.mapElse(source, PrizeVO::getUsedCount, 0));
		vo.setUsedFreeCount(X.mapElse(source, PrizeVO::getUsedFreeCount, 0));
		vo.setUsedFreeTimes(X.mapElse(source, PrizeVO::getUsedFreeTimes, 0));
		vo.setTradeTotalAmount(X.mapElse(source, PrizeVO::getTradeTotalAmount, BigDecimal.ZERO));
		vo.setTradeTotalFreePart(X.mapElse(source, PrizeVO::getTradeTotalFreePart, BigDecimal.ZERO));
		vo.setTradeRechargeAmount(vo.getTradeTotalAmount().subtract(vo.getTradeTotalFreePart()));
		vo.setTotalAmount(X.mapElse(source, PrizeVO::getTotalAmount, 0));
		return vo;
	}

	public String getTradeTotalAmount_() {
		return Formats.formatNumber_D(tradeTotalAmount);
	}

	public String getTradeRechargeAmount_() {
		return Formats.formatNumber_D(tradeRechargeAmount);
	}

	public String getTradeTotalFreePart_() {
		return Formats.formatNumber_D(tradeTotalFreePart);
	}

	public String getLevel_() {
		return level != null ? level + "级" : null;
	}

	public String getGiftType_() {
		return X.map(giftType, GiftType::getLabel);
	}

}
